package com.ruoyi.web.controller.quality;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.vo.DefectClassVo;
import com.ruoyi.quality.domain.bo.DefectClassBo;
import com.ruoyi.quality.service.IDefectClassService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问题分类
 *
 * @author ruoyi
 * @date 2023-08-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/class")
public class DefectClassController extends BaseController {

    private final IDefectClassService iDefectClassService;

    /**
     * 查询问题分类列表
     */
    @SaCheckPermission("quality:class:list")
    @GetMapping("/list")
    public TableDataInfo<DefectClassVo> list(DefectClassBo bo, PageQuery pageQuery) {
        return iDefectClassService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询问题分类列表
     */
    @SaCheckPermission("quality:class:list")
    @GetMapping("/query")
    public List<Object> query(DefectClassBo bo) {
        return iDefectClassService.queryClassList(bo);
    }

    /**
     * 导出问题分类列表
     */
    @SaCheckPermission("quality:class:export")
    @Log(title = "问题分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DefectClassBo bo, HttpServletResponse response) {
        List<DefectClassVo> list = iDefectClassService.queryList(bo);
        ExcelUtil.exportExcel(list, "问题分类", DefectClassVo.class, response);
    }

    /**
     * 获取问题分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:class:query")
    @GetMapping("/{id}")
    public R<DefectClassVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(iDefectClassService.queryById(id));
    }

    /**
     * 新增问题分类
     */
    @SaCheckPermission("quality:class:add")
    @Log(title = "问题分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DefectClassBo bo) {
        return toAjax(iDefectClassService.insertByBo(bo));
    }

    /**
     * 修改问题分类
     */
    @SaCheckPermission("quality:class:edit")
    @Log(title = "问题分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DefectClassBo bo) {
        return toAjax(iDefectClassService.updateByBo(bo));
    }

    /**
     * 删除问题分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:class:remove")
    @Log(title = "问题分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(iDefectClassService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
